package cn.wtg.kotlinlib.bindingAdapter

import android.view.View
import androidx.databinding.BindingAdapter
import androidx.annotation.DrawableRes
import androidx.annotation.FloatRange
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.ColorUtils

/**
 * author：wtg
 * time：2020/3/15
 * desc：提取操作的公共部分
 */
object CommonBindingAdapter {
    /**
     * 设置设置背景颜色值/背景图 drawable
     *
     * @param view    view
     * @param binding_background_drawable_res 资源id
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_background_drawable_res"], requireAll = false)
    fun setBgDrawableRes(
        view: View,
        @DrawableRes binding_background_drawable_res: Int
    ) {
        view.setBackgroundResource(binding_background_drawable_res)
    }

    /**
     * 设置设置背景颜色值/背景图 colorRes
     *
     * @param view    view
     * @param binding_background_color_res 资源id
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_background_color_res"], requireAll = false)
    fun setBgColorRes(
        view: View,
        @ColorRes binding_background_color_res: Int,
    ) {
        view.setBackgroundColor(ColorUtils.getColor(binding_background_color_res))
    }

    /**
     * 设置背景的透明度
     * @param view 控件
     * @param binding_alpha 透明度 0完全透明 1完全不透明
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_alpha"],requireAll = false)
    fun setBgAlpha(view: View,@FloatRange(from = 0.0, to = 1.0) binding_alpha: Float){
        view.background.mutate().alpha = (binding_alpha * 255).toInt()
    }

    /**
     * 设置字体颜色
     *
     * @param textView   view
     * @param binding_text_color 资源id
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_text_color"], requireAll = false)
    fun setTextColor(textView: TextView, @ColorRes binding_text_color: Int) {
        textView.setTextColor(ColorUtils.getColor(binding_text_color))
    }

    /**
     * 设置透明度
     *
     * @param textView view
     * @param binding_alpha    透明度
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_alpha"], requireAll = false)
    fun setTextAlpha(textView: TextView, @FloatRange(from = 0.0, to = 1.0) binding_alpha: Float) {
        textView.alpha = binding_alpha
    }

    /**
     * 设置hint字体颜色
     *
     * @param textView   所有继承AppCompatTextView 的类
     * @param binding_text_hint_color 颜色id
     */
    @JvmStatic
    @BindingAdapter("binding_text_hint_color")
    fun setTextHintColor(textView: AppCompatTextView, @ColorRes binding_text_hint_color: Int) {
        textView.setHintTextColor(ColorUtils.getColor(binding_text_hint_color))
    }
}